package zisu.rabbit.rabbitmqspring.convert;

import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.support.converter.MessageConversionException;
import org.springframework.amqp.support.converter.MessageConverter;

/**
 * @Author: 蔡翔
 * @Date: 2019/8/14 9:58
 * @Version 1.0
 */
public class TextMessageConvert implements MessageConverter {
    @Override
    public Message toMessage(Object object, MessageProperties messageProperties) throws MessageConversionException {
        return new Message(object.toString().getBytes(),messageProperties);
    }

    @Override
    public Object fromMessage(Message message) throws MessageConversionException {
        String contentType = message.getMessageProperties().getContentType();
        if(null != contentType && contentType.contains("text")){
            //contentType.contains("text")意思就是请求头中 是否包含text
            return new String(message.getBody());
        }
        return message.getBody();
    }
}
